import AbstractRuntimeView from "../../../common/abstract/mvc/AbstractRuntimeView";
import Event from "../../../common/manager/EventClick";
import { UIMgr } from "../../../common/manager/viewMgr";
import { ZYEventMgr } from "../../../common/manager/YLEventMgr";
import ArrayUtils from "../../../common/util/ArrayUtils";
import MyTools from "../../../common/util/MyTools";
import { ADMgr } from "../../../common/wxComponent/ADManager";

/**
 * 
 * @author: xj.huang
 * @date: 2021/08/10 18:11:47
 */
export default class GameWinView  extends AbstractRuntimeView<null, null>{
    private winList:Laya.List;
    private pageInfo;
    private bg:Laya.Image;
    private BackFirst:Laya.Image;
    private viewType = "GameStartView";
    private winOrLoseTips:Laya.Image;
    constructor() {
        super()
    }
    onOnce(param) {
        this.pageInfo = this.gameConfig.ad_move_conf.page.end;
        MyTools.ChangeBGColor(this.bg, this.pageInfo.color)
        this.gameConfig.box_conf.endToHome.currIndex=0;
        this.addEvent()
        this.onShowIcon()
        this.onShowVideo()
        
    }
    private async onShowVideo(){
        if(this.gameConfig.ad_move_conf.is_show===0) return;
        this.BackFirst.visible = false;
        await ADMgr.wxVideoAd.showVoide(3)
        this.BackFirst.visible = true;
    }
    /**显示失败或者成功ICON */
    private onShowIcon(){
        if(this.param.isScuees){
            this.winOrLoseTips.skin = "ZhiYingRes/DaoChu/texture/Win_win.png"
        }else{
            this.winOrLoseTips.skin = "ZhiYingRes/DaoChu/texture/Win_lose.png"
        }
    }
    private addEvent() {
        this.winList.renderHandler = new Laya.Handler(this, this.onupItem);
        this.winList.vScrollBarSkin = "";
        Event.onClick(this.BackFirst, this, this.onCloesView);
        this.onupItemdata(this.gameConfig.game_box)
    }
    onEnable() {
        super.onEnable();
        ADMgr.wxAdRipoff.isShowBtn(this.BackFirst, this.pageInfo, MyTools.getRandomPer(this.gameConfig.box_conf.end.is_banner));
        // console.log("this.param",this.param,this.pageInfo)
    }
    private onupItemdata(data = []) {
        let _data = ArrayUtils.conleData(data) as any[];
        for (let k in _data) {
            if (_data[k]['isHot'] == undefined) _data[k]['isHot'] = this.getIsHot(_data[k]['is_hot'])
        }
        this.winList.array = _data;
        this.winList.refresh();
    }
    private onupItem(cell: Laya.Box, index) {
        let gameicon = cell.getChildByName("gameicon") as Laya.Image;
        let NameBG = cell.getChildByName("bg") as Laya.Image;
        let hotOrNew = cell.getChildByName("hot_or_new") as Laya.Image;
        let hand = cell.getChildByName("hand") as Laya.Box;
        cell["appId"] = cell.dataSource.app_id;
        gameicon.skin = cell.dataSource.icon_url;
        hotOrNew.visible = cell.dataSource.isHot
        hotOrNew.skin = `ZhiYingRes/DaoChu/texture/Hot_hot${MyTools.getRandomNumInt(1, 3)}.png`;
        hand.visible = cell.dataSource.isHot
        Event.offClick(cell, this, this.onToApp)
        if (this.gameConfig.box_conf.force_ItemCount == 0) {
            Event.onMouse(cell, this, this.onToApp,[cell.dataSource])
        } else {
            Event.onClick(cell, this, this.onToApp,[cell.dataSource])
        }
    }
    private onToApp(dataSource) {
        ADMgr.openWxAPP(dataSource.app_id,dataSource.path,dataSource)
    }
    private getIsHot(v) {
        if (v != 0) {
            if (Math.random() <= this.gameConfig.box_conf.hand_and_icon.hand / 100) {
                return true
            }
            return false
        }
        return false
    }
    async onCloesView(){
        await this.asyncExportOpen(this.gameConfig.box_conf.endToHome,this.viewType,this.allViewColse.bind(this))
        Laya.timer.once(100,this,this.onClose)
    }
    async allViewColse(){
        UIMgr.close(ViewRes.VIEW_GAME)
        await this.UIMgrAsyncOpen(ViewRes.VIEW_GAMESTART,{type:"endView"}) 
    }
}